﻿CREATE PROCEDURE [dbo].[Hogar_ParteQuincenalPersonal_ObtenerPorMesQuincena]
	@Mes INT,
	@Año INT,
	@Quincena INT
AS
BEGIN
		DECLARE @ParteQuincenalPersonalId INT

		SELECT @ParteQuincenalPersonalId = [ParteQuincenalPersonalId]
		FROM [dbo].[ParteQuincenalPersonal] 
		WHERE [Mes] = @Mes AND [Año] = @Año AND [Quincena] = @Quincena

		SELECT 	[ParteDiarioPersonalId],
			pdp.[AgenteId],
			[DiaJustificar],
			[PeriodoDesde],
			[PeriodoHasta],
			ISNULL([Observaciones],'') AS [Observaciones],
			pdp.[CodigoAusentismoId],
			[ParteQuincenalPersonalId],
			co.[Descripcion] AS [Codigo],
			ca.[Codigo] AS [CatCar],
			ag.[Apellido],
			ag.[Nombre],
			ag.[Dni],
			@Mes AS [Mes],
			@Año AS [Año],
			@Quincena AS [Quincena],
			CASE WHEN pdp.[Activo] = 1 THEN 1 ELSE 0 END AS [Activo]
		FROM [dbo].[ParteDiarioPersonal] pdp WITH (NOLOCK)
		INNER JOIN [dbo].[Agente] ag WITH (NOLOCK) ON ag.[AgenteId] = pdp.[AgenteId]
		INNER JOIN [dbo].[CodigoAusentismo] co WITH (NOLOCK) ON co.[CodigoAusentismoId] = pdp.[CodigoAusentismoId]
		INNER JOIN [dbo].[Cargo] ca WITH (NOLOCK) ON ca.[CargoId] = ag.[CargoId]
		WHERE [ParteQuincenalPersonalId] = @ParteQuincenalPersonalId AND pdp.[Activo] = 1
		ORDER BY ag.[Apellido], ag.[Nombre], [DiaJustificar] DESC, [PeriodoDesde]

END
